<!DOCTYPE HTML>
<html lang="zh-CN">


<head><meta name="generator" content="Hexo 3.8.0">
    <meta charset="utf-8">
    <meta name="keywords" content="Flutter开发-Dart语言基础, Swift, iOS, Python">
    <meta name="description" content="群组欢迎加入群组,闲聊工作&amp;amp;技术&amp;amp;问题等

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dart是一种易于学习,易于扩展,并且可以部署到任何地方的应用编程语言.Dart是面">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>Flutter开发-Dart语言基础 | waitwalker</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">
    <style type="text/css">
        
    </style>

    <script src="/libs/jquery/jquery-2.2.0.min.js"></script>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>


<body>

<!--导航-->

<header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    <!---->
                    <!--<img src="/medias/logo.png" class="logo-img" alt="LOGO">-->
                    <!---->
                    <span class="logo-span">waitwalker</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fa fa-navicon"></i></a>
<ul class="right">
    
    <li class="hide-on-med-and-down">
        <a href="/" class="waves-effect waves-light">
            
            <i class="fa fa-home"></i>
            
            <span>首页</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/tags" class="waves-effect waves-light">
            
            <i class="fa fa-tags"></i>
            
            <span>标签</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/categories" class="waves-effect waves-light">
            
            <i class="fa fa-bookmark"></i>
            
            <span>分类</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/archives" class="waves-effect waves-light">
            
            <i class="fa fa-archive"></i>
            
            <span>归档</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/about" class="waves-effect waves-light">
            
            <i class="fa fa-user-circle-o"></i>
            
            <span>关于</span>
        </a>
    </li>
    
    <li class="hide-on-med-and-down">
        <a href="/friends" class="waves-effect waves-light">
            
            <i class="fa fa-address-book"></i>
            
            <span>友情链接</span>
        </a>
    </li>
    
    <li>
        <a href="#searchModal" class="modal-trigger waves-effect waves-light">
            <i id="searchIcon" class="fa fa-search" title="搜索"></i>
        </a>
    </li>
</ul>

<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">waitwalker</div>
        <div class="logo-desc">
            
            Life is measured by thought and action not by time.
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li>
            <a href="/" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-home"></i>
                
                首页
            </a>
        </li>
        
        <li>
            <a href="/tags" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-tags"></i>
                
                标签
            </a>
        </li>
        
        <li>
            <a href="/categories" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-bookmark"></i>
                
                分类
            </a>
        </li>
        
        <li>
            <a href="/archives" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-archive"></i>
                
                归档
            </a>
        </li>
        
        <li>
            <a href="/about" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-user-circle-o"></i>
                
                关于
            </a>
        </li>
        
        <li>
            <a href="/friends" class="waves-effect waves-light">
                
                <i class="fa fa-fw fa-address-book"></i>
                
                友情链接
            </a>
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/waitwalker" class="waves-effect waves-light" target="_blank">
                <i class="fa fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>

        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/waitwalker" class="github-corner tooltipped hide-on-med-and-down" target="_blank" data-tooltip="https://github.com/waitwalker" data-position="left" data-delay="50">
    <svg viewbox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"/>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"/>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"/>
    </svg>
</a>
        
    </nav>

</header>





<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/8.jpg')">
    <div class="container">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <div class="description center-align post-title">
                        Flutter开发-Dart语言基础
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>



<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #toc-content .is-active-link::before {
        background-color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 20px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/Flutter-开发/" target="_blank">
                                <span class="chip bg-color">Flutter 开发</span>
                            </a>
                        
                            <a href="/tags/Dart/" target="_blank">
                                <span class="chip bg-color">Dart</span>
                            </a>
                        
                            <a href="/tags/iOS/" target="_blank">
                                <span class="chip bg-color">iOS</span>
                            </a>
                        
                            <a href="/tags/Android/" target="_blank">
                                <span class="chip bg-color">Android</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fa fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/Flutter开发/" class="post-category" target="_blank">
                                Flutter开发
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                <div class="post-date info-break-policy">
                    <i class="fa fa-calendar-minus-o fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2019-06-11
                </div>

                
				
				
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="fa fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <p><strong>群组</strong><br><strong>欢迎加入群组,闲聊工作&amp;技术&amp;问题等</strong></p>
<p><img src="https://github.com/waitwalker/Resources/blob/master/Flutter/group/flutter_development_0923.JPG?raw=true" width="350" height="480" align="center"></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dart是一种易于学习,易于扩展,并且可以部署到任何地方的应用编程语言.Dart是面向对象的编程语言,由Google在2011推出.</p>
<h3 id="相关概念"><a href="#相关概念" class="headerlink" title="相关概念:"></a>相关概念:</h3><p>1)Dart真正实现了”一切皆对象”,即使数值类型,函数方法和null都是对象,上帝类是Object类.<br>2)Dart是强类型语言,类型标识是可选的,Dart会自动推断变量的具体类型.如果要指明某个对象为不确定类型,可以用dynamic(动态性)关键字来修饰变量.<br>3)Dart支持泛型,和Swift类似.<br>4)Dart支持顶级函数(main函数等,也是程序的入口),静态方法,实例方法.支持嵌套函数(函数内部创建函数)等.<br>5)Dart没有public,private等关键字,默认一些变量和方法都是公有的,如果想声明私有变量或者方法,可以用”_(下户线)”开头声明.<br>6)Dart可以使用分号作为语句的结束,也可以不写.<br>7)Dart注释.//和/**/</p>
<h3 id="变量"><a href="#变量" class="headerlink" title="变量"></a>变量</h3><h4 id="变量的定义用var关键字-类型可以不写"><a href="#变量的定义用var关键字-类型可以不写" class="headerlink" title="变量的定义用var关键字,类型可以不写:"></a>变量的定义用var关键字,类型可以不写:</h4><pre><code>var name = &quot;flutter&quot;;// String类型
var age = 20; // int 类型
var height = 1.80; // Double 类型
</code></pre><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;当给变量赋值的时候,变量的类型已经确定,此时不能给变量赋值其他类型值:</p>
<pre><code>var name = &quot;flutter&quot;; //String
name = 12;// 此时报错
</code></pre><p><img src="https://github.com/waitwalker/Resources/blob/master/Flutter/Dart/var%20error.png?raw=true" alt="var error"></p>
<h4 id="可以使用特定类型来定义变量"><a href="#可以使用特定类型来定义变量" class="headerlink" title="可以使用特定类型来定义变量"></a>可以使用特定类型来定义变量</h4><pre><code>String newName = &quot;my flutter&quot;;
int newAge = 21;
double newHeighth = 1.85;
</code></pre><h4 id="变量的类型如果可变-可以用dynamic或者Obejct关键字来定义变量"><a href="#变量的类型如果可变-可以用dynamic或者Obejct关键字来定义变量" class="headerlink" title="变量的类型如果可变,可以用dynamic或者Obejct关键字来定义变量"></a>变量的类型如果可变,可以用dynamic或者Obejct关键字来定义变量</h4><pre><code>dynamic flu = &quot;Flutter&quot;; //String类型
print(&quot;flu的值为:$flu&quot;);
flu = 12; //int 类型
print(&quot;flu的值为:$flu&quot;);

I/flutter (25846): 开始点击count
I/flutter (25846): 点击完成count=1
I/flutter (25846): flu的值为:Flutter
I/flutter (25846): flu的值为:12

Object da = &quot;Dart&quot;;
print(&quot;da的值为:$da&quot;);

da = 2.0;
print(&quot;da的值为:$da&quot;);
I/flutter (25846): da的值为:Dart
I/flutter (25846): da的值为:2.0
</code></pre><h4 id="变量默认值"><a href="#变量默认值" class="headerlink" title="变量默认值"></a>变量默认值</h4><p>如果声明一个变量而没有给他赋值的话,其默认值是null,null在Flutter中也是一个对象.</p>
<pre><code>var stuName;
print(&quot;stu name:$stuName&quot;);
dynamic stuNewName;
print(&quot;stu new name:$stuNewName&quot;);

I/flutter (25846): stu name:null
I/flutter (25846): stu new name:null
</code></pre><h4 id="class关键字"><a href="#class关键字" class="headerlink" title="class关键字"></a>class关键字</h4><p>class关键字用于创建一个类,如果不指明继承哪个类,默认继承上帝类Object</p>
<pre><code>class Student extends Object {

}
</code></pre><h3 id="常量"><a href="#常量" class="headerlink" title="常量"></a>常量</h3><h4 id="final-和-const"><a href="#final-和-const" class="headerlink" title="final 和 const"></a>final 和 const</h4><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如果一个想定义一个常量,即一个变量的值被赋值后一直不会改变,可以通过final和const来声明,final修饰的变量在声明时不赋值,而在对象初始化时赋值;const修饰的变量必须赋值,否则报错.final 类似于Swift或者OC中的只读属性.</p>
<pre><code>class Student extends Object {
  static const constVar; //const 修饰的变量必须赋值
  final name;
  Student(this.name);
}

// 使用
Student stu_1 = Student(&quot;waitwalker&quot;);
print(&quot;stu_1 name:${stu_1.name}&quot;);
I/flutter (25846): stu_1 name:waitwalker
</code></pre><p><img src="https://github.com/waitwalker/Resources/blob/master/Flutter/Dart/final%20error.png?raw=true" alt="final error"></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const 修饰的变量赋值后就不会报错了,此时如果给stu_1.name重新赋值会报错,final修饰的变量不允许调用setter方法:</p>
<pre><code>class Student extends Object {
  static const constVar = &quot;const 修饰的变量必须赋值&quot;; //const 修饰的变量必须赋值
  final name;
  Student(this.name);
}

// 使用
Student stu_1 = Student(&quot;waitwalker&quot;);
print(&quot;stu_1 name:${stu_1.name}&quot;);
stu_1.name = &quot;new&quot;;
</code></pre><p><img src="https://github.com/waitwalker/Resources/blob/master/Flutter/Dart/const%20error.png?raw=true" alt="const error"></p>
<h3 id="Dart标准库中内置类型"><a href="#Dart标准库中内置类型" class="headerlink" title="Dart标准库中内置类型"></a>Dart标准库中内置类型</h3><h4 id="判断对象的类型"><a href="#判断对象的类型" class="headerlink" title="判断对象的类型"></a>判断对象的类型</h4><p>在上帝类中有个只读属性runtimeType用于获取对象的类型:</p>
<pre><code>print(&quot;stu_1 type:${stu_1.runtimeType}&quot;);
print(&quot;stu_1 type:${stu_1.name.runtimeType}&quot;);

I/flutter (25846): stu_1 type:Student
I/flutter (25846): stu_1 type:String
</code></pre><h4 id="数值类型num"><a href="#数值类型num" class="headerlink" title="数值类型num"></a>数值类型num</h4><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num是Dart基本数值类型,其是int和doule的父类,Dart只有整型和浮点型两种基本数据类型,其长度依据操作系统自由伸缩:</p>
<pre><code>abstract class int extends num {
}

abstract class double extends num {
}
</code></pre><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;可以看到int类在num基本的运算操作基础上增加了一些位运算方法,double增加了一些如取绝对值等方法.</p>
<h4 id="字符串String"><a href="#字符串String" class="headerlink" title="字符串String"></a>字符串String</h4><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String是charCodes字符的集合,charCodes默认采用的是UTF-16编码,一个String在定义时可以采用单引号或者双引号将内容包起来.</p>
<pre><code>abstract class String implements Comparable&lt;String&gt;, Pattern {

}
</code></pre><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如果多行字符串,可以采用三重(单/双)引号,将内容包起来,这个类似于Python和Swift的相关语法.</p>
<pre><code>String today = &quot;&quot;&quot;
Today is very hot.
Yes.
No.
&quot;&quot;&quot;;
print(&quot;toady:$today&quot;);

I/flutter (26474): toady: Today is very hot.
I/flutter (26474): Yes.
I/flutter (26474): No.
</code></pre><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String类中还定义了一些截串,获取索引,比较两个字符串,获取长度等一些常规方法.</p>
<h4 id="布尔类型bool"><a href="#布尔类型bool" class="headerlink" title="布尔类型bool"></a>布尔类型bool</h4><p>bool类型很简单,非零即真:</p>
<pre><code>bool isiPad = false;
print(&quot;is iPad:$isiPad&quot;);
I/flutter (26474): is iPad:false
</code></pre><h4 id="集合类型List"><a href="#集合类型List" class="headerlink" title="集合类型List"></a>集合类型List</h4><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dart中集合类型List和Python中的List可以说一脉相承,List是一个泛型集合,可以存储任何需要的对象类型,其常规的增删改查操作方法和其他语言集合类型相似.</p>
<pre><code>List list = [&quot;date&quot;,12,45.0,&quot;weather&quot;]; //初始化方式
List list_a = List(10); //指定长度初始化
list_a.add(&quot;12&quot;); //添加操作
list_a.add(12);
list_a.removeAt(1); //移除指定索引位置元素
list_a.removeLast(); //移除最后一个元素
</code></pre><h4 id="字典类型-键值类型-Map"><a href="#字典类型-键值类型-Map" class="headerlink" title="字典类型(键值类型)Map"></a>字典类型(键值类型)Map</h4><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dart中的Map类型和其他语言中Map或者Dictionary中相似,在定义的时候可以指定键值得类型.</p>
<pre><code>Map dic = Map();
dic[&quot;name&quot;] = &quot;flutter&quot;;
dic[&quot;version&quot;] = &quot;1.2&quot;;
Map dic_a = {
&quot;name&quot;:&quot;flutter&quot;,
&quot;version&quot;:&quot;1.2&quot;
};

print(&quot;dic:$dic&quot;);
print(&quot;dic_a:$dic_a&quot;);

I/flutter (26474): dic:{name: flutter, version: 1.2}
I/flutter (26474): dic_a:{name: flutter, version: 1.2}
</code></pre><h4 id="Runes类型"><a href="#Runes类型" class="headerlink" title="Runes类型"></a>Runes类型</h4><p>Runes类型一般用于编码特殊的Unicode,例如emoji.</p>
<h3 id="方法"><a href="#方法" class="headerlink" title="方法"></a>方法</h3><p>方法是程序功能的集合.</p>
<h4 id="方法的常规定义"><a href="#方法的常规定义" class="headerlink" title="方法的常规定义"></a>方法的常规定义</h4><pre><code>返回值类 方法名() {
return value;
}

// 例如
String getName() {
return name;
}

Student stu_1 = Student(&quot;waitwalker&quot;);
print(&quot;stu_1 name:${stu_1.name}&quot;);
String newN = stu_1.getName();
print(&quot;newN:$newN&quot;);

I/flutter (26474): stu_1 name:waitwalker
I/flutter (26474): newN:waitwalker
</code></pre><h4 id="方法参数"><a href="#方法参数" class="headerlink" title="方法参数"></a>方法参数</h4><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dart方法的参数可以是变量也可以是一个方法,如果方法参数是一个变量,可以指定变量类型也可以不指定,建议指定.</p>
<p>方法中也可以设定可选参数,可选参数用[]包起来.</p>
<pre><code>// 显示名称
String showName(String firstName) {
  return name + firstName;
}

Student student = Student(&quot;xiaoming&quot;);
String fullName = student.showName(&quot;wang&quot;);
print(&quot;full name:$fullName&quot;);

I/flutter (26474): full name:xiaomingwang
</code></pre><h4 id="方法作为参数"><a href="#方法作为参数" class="headerlink" title="方法作为参数"></a>方法作为参数</h4><p>Dart允许在方法中传递参数,其实传递的是方法的地址:</p>
<pre><code>int sum(int a, int b) {
  print(&quot;sum result:${a + b}&quot;);
  return a + b;
}

int toatl(int a, int b, Function getSum) {
  return getSum(a,b);
}

void result() {
int r = toatl(12, 18, sum);
print(&quot;result:$r&quot;);
}

Student student = Student(&quot;xiaoming&quot;);
String fullName = student.showName(&quot;zhang&quot;,12);
print(&quot;full name:$fullName&quot;);
student.result();

I/flutter (26474): sum result:30
I/flutter (26474): result:30
</code></pre><h4 id="方法参数默认值"><a href="#方法参数默认值" class="headerlink" title="方法参数默认值"></a>方法参数默认值</h4><p>在定义方法时,可选参数也可以指定一个默认值:</p>
<pre><code>// 显示名称
String showName(String firstName, [int age = 20]) {
return name + firstName + &quot;age is&quot; + age.toString();
}

Student student = Student(&quot;xiaoming&quot;);
String fullName = student.showName(&quot;zhang&quot;,12);
print(&quot;full name:$fullName&quot;);
I/flutter (26474): full name:xiaomingzhang age is12
</code></pre><h4 id="方法中嵌套方法"><a href="#方法中嵌套方法" class="headerlink" title="方法中嵌套方法"></a>方法中嵌套方法</h4><p>Dart语法方法中是可以嵌套方法的:</p>
<pre><code>void size(int a) {
void intervalSize () {
print(&quot;size in:$a&quot;);
}

print(&quot;size out:$a&quot;);
}
</code></pre><h4 id="匿名函数"><a href="#匿名函数" class="headerlink" title="匿名函数"></a>匿名函数</h4><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dart中匿名函数和Python中的lamada匿名函数一样,不用给方法起名字了,在定义的时候直接用一个变量来接收,引用时直接引用这个变量:</p>
<pre><code>var lamadaFunc = (String name) =&gt; &quot;${name}&quot;;
student.lamadaFunc(&quot;wangxiaoming&quot;);
</code></pre><h4 id="闭包"><a href="#闭包" class="headerlink" title="闭包"></a>闭包</h4><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;闭包是对匿名函数的一种应用,Dart 其在Function类型,而在Swift中常当作函数参数用于一些异步回调,在Dart定义:</p>
<pre><code>Function totalNum(int a) {
return (int b) =&gt; a + b;
}

var totalNum = student.totalNum(12);
var value = totalNum(3);
print(&quot;value:$value&quot;);
I/flutter (26474): value:15
</code></pre>
            </div>
            <hr>

            
            <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.88rem;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-large waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fa fa-close"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>
            

            <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">

<div id="article-share">
    
    <div class="social-share" data-disabled="qzone" data-wechat-qrcode-helper="<p>微信里点“发现”->“扫一扫”二维码便可查看分享。</p>"></div>
    
</div>

<script src="/libs/share/js/social-share.min.js"></script>

            <div class="reprint">
                <p>
                    <span class="reprint-tip">
                        <i class="fa fa-exclamation-circle"></i>&nbsp;&nbsp;转载请注明:
                    </span>
                    <a href="https://waitwalker.github.io" class="b-link-green">waitwalker</a>
                    <i class="fa fa-angle-right fa-lg fa-fw text-color"></i>
                    <a href="/2019/06/11/Flutter开发-Dart语言基础/" class="b-link-green">Flutter开发-Dart语言基础</a>
                </p>
            </div>
        </div>
    </div>

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6 overflow-policy" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fa fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2019/06/12/Flutter开发-Dart语言基础-2/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/13.jpg" class="responsive-img" alt="Flutter开发-Dart语言基础-2">
                        
                        <span class="card-title">Flutter开发-Dart语言基础-2</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">群组欢迎加入群组,闲聊工作&amp;技术&amp;问题等

接着上篇Dart语言基础.
运算符Dart和其他语言一样多种,支持多种运算.
点运算语法糖&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb</div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="fa fa-clock-o fa-fw icon-date"></i>2019-06-12
                        </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/Flutter开发/" class="post-category" target="_blank">
                                    Flutter开发
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Flutter-开发/" target="_blank">
                        <span class="chip bg-color">Flutter 开发</span>
                    </a>
                    
                    <a href="/tags/Dart/" target="_blank">
                        <span class="chip bg-color">Dart</span>
                    </a>
                    
                    <a href="/tags/iOS/" target="_blank">
                        <span class="chip bg-color">iOS</span>
                    </a>
                    
                    <a href="/tags/Android/" target="_blank">
                        <span class="chip bg-color">Android</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6 overflow-policy" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fa fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2019/06/10/Mac-Flutter-环境搭建/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/2.jpg" class="responsive-img" alt="Mac Flutter 环境搭建">
                        
                        <span class="card-title">Mac Flutter 环境搭建</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">群组欢迎加入群组,闲聊工作&amp;技术&amp;问题等

以下内容是再Mac上搭建Flutter开发环境的过程:
1. 添加环境变量Mac 一般使用bash作为默认shellMac系统的环境变量，加载顺序为：/etc/profile/et</div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="fa fa-clock-o fa-fw icon-date"></i>2019-06-10
                            </span>
                        <span class="publish-author">
                            
                            <i class="fa fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/Flutter开发/" class="post-category" target="_blank">
                                    Flutter开发
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Flutter-开发/" target="_blank">
                        <span class="chip bg-color">Flutter 开发</span>
                    </a>
                    
                    <a href="/tags/Dart/" target="_blank">
                        <span class="chip bg-color">Dart</span>
                    </a>
                    
                    <a href="/tags/iOS/" target="_blank">
                        <span class="chip bg-color">iOS</span>
                    </a>
                    
                    <a href="/tags/Android/" target="_blank">
                        <span class="chip bg-color">Android</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>
</div>


<script>
    $('#articleContent').on('copy', function (e) {
        // IE8 or earlier browser is 'undefined'
        if (typeof window.getSelection === 'undefined') return;

        var selection = window.getSelection();
        // if the selection is short let's not annoy our users.
        if (('' + selection).length < Number.parseInt('120')) {
            return;
        }

        // create a div outside of the visible area and fill it with the selected text.
        var bodyElement = document.getElementsByTagName('body')[0];
        var newdiv = document.createElement('div');
        newdiv.style.position = 'absolute';
        newdiv.style.left = '-99999px';
        bodyElement.appendChild(newdiv);
        newdiv.appendChild(selection.getRangeAt(0).cloneContents());

        // we need a <pre> tag workaround.
        // otherwise the text inside "pre" loses all the line breaks!
        if (selection.getRangeAt(0).commonAncestorContainer.nodeName === 'PRE') {
            newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>";
        }

        var url = document.location.href;
        newdiv.innerHTML += '<br />'
            + '来源: waitwalker<br />'
            + '作者: waitwalker<br />'
            + '链接: <a href="' + url + '">' + url + '</a><br />'
            + '本文章著作权归作者所有，任何形式的转载都请注明出处。';

        selection.selectAllChildren(newdiv);
        window.setTimeout(function () {bodyElement.removeChild(newdiv);}, 200);
    });
</script>

    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="fa fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fa fa-list"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            // headingsOffset: -205,
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).slideUp(500);
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).slideDown(500);
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>
    

</main>


<footer class="page-footer bg-color">
    <div class="container row center-align">
        <div class="col s12 m8 l8 copy-right">
            本站在原作者&copy;<a href="https://blinkfox.github.io/" target="_blank">Blinkfox</a>基于
            <a href="https://hexo.io/" target="_blank">Hexo</a> 的
            <a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">hexo-theme-matery</a>主题基础上搭建.

            

            
			
                <br>
                
                <span id="busuanzi_container_site_pv">
                    <i class="fa fa-heart-o"></i>
                    本站总访问量 <span id="busuanzi_value_site_pv" class="white-color"></span>
                </span>
                
                
                <span id="busuanzi_container_site_uv">
                    <i class="fa fa-users"></i>
                    次,&nbsp;访客数 <span id="busuanzi_value_site_uv" class="white-color"></span> 人.
                </span>
                
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis"><!---->
<a href="https://github.com/waitwalker" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
    <i class="fa fa-github"></i>
</a>

<a href="https://twitter.com/284485487" class="tooltipped" target="_blank" data-tooltip="Twitter联系我" data-position="top" data-delay="50">
    <i class="fa fa-twitter"></i>
</a>

<a href="https://www.facebook.com/profile.php?id=100003684945279" class="tooltipped" target="_blank" data-tooltip="Twitter联系我" data-position="top" data-delay="50">
    <i class="fa fa-facebook"></i>
</a>

<a href="https://t.me/joinchat/Ej0o0A1ntlq5ZFIMzzO5Pw" class="tooltipped" target="_blank" data-tooltip="Twitter联系我" data-position="top" data-delay="50">
    <i class="fa fa-telegram"></i>
</a>

<a href="https://www.weibo.com/1789419613/profile?topnav=1&wvr=6&is_all=1" class="tooltipped" target="_blank" data-tooltip="Twitter联系我" data-position="top" data-delay="50">
    <i class="fa fa-weibo"></i>
</a>


<!--原来-->
<!---->
    <!--<a href="mailto:waitwalker@163.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">-->
        <!--<i class="fa fa-envelope-open"></i>-->
    <!--</a>-->
<!---->

<!---->
    <!--<a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=284485487" class="tooltipped" data-tooltip="QQ联系我: 284485487" data-position="top" data-delay="50">-->
        <!--<i class="fa fa-qq"></i>-->
    <!--</a>-->
<!---->

<!---->
    <!--<a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">-->
        <!--<i class="fa fa-rss"></i>-->
    <!--</a>-->
<!----></div>
    </div>
</footer>

<div class="progress-bar"></div>


<!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fa fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字" class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc("/" + "search.xml", 'searchInput', 'searchResult');
});
</script>
<!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fa fa-angle-up"></i>
    </a>
</div>


<script src="/libs/materialize/materialize.min.js"></script>
<script src="/libs/masonry/masonry.pkgd.min.js"></script>
<script src="/libs/aos/aos.js"></script>
<script src="/libs/scrollprogress/scrollProgress.min.js"></script>
<script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
<script src="/js/matery.js"></script>

<!-- Global site tag (gtag.js) - Google Analytics -->




    <script async src="/libs/others/busuanzi.pure.mini.js"></script>


</body>
</html>